<template>
  <a-modal
      title="添加SUK排斥"
      :width="720"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :maskClosable="false"
      :destroyOnClose="true"
      @ok="handleSubmit"
      @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <div v-for="(item, index) in categoryList" style="padding: 5px 0;">
        {{item.spec_name}}：
        <a-radio-group v-model:value="categoryIndex[index]" button-style="solid" >
          <a-radio-button v-for="(item2, index2) in item.valueList" :key="item2.spec_value_id"  :value="item2.spec_value_id">
            {{item2.spec_value}}
          </a-radio-button>
        </a-radio-group>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import PropTypes from 'ant-design-vue/es/_util/vue-types'
import * as GoodsApi from '@/api/goods'

export default {
  props: {
    // 默认的规格列表
    categoryList: PropTypes.array.def([]),
    goodsId: PropTypes.string.def('')
  },

  data() {
    return {
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      categoryIndex:[]
    }
  },

  methods: {
    /**
     * 显示对话框
     */
    add() {
      // 显示窗口
      this.visible = true;
      this.categoryIndex=[];
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      this.onFormSubmit()
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.confirmLoading = true
      let filteredArr = this.categoryIndex.filter(function(value) {
        return value !== undefined && value !== null && value !== '';
      });
      const data = {
        goodsId:this.goodsId,
        spec_value_ids :filteredArr.join(',')
      }
      GoodsApi.setexclusion({form:data})
          .then((result) => {
            // 显示成功
            this.$message.success(result.message, 1.5)
            // 关闭对话框事件
            this.handleCancel()
            // 通知父端组件提交完成了
            this.$emit('handleSubmit', values)
          })
          .finally((result) => {
            this.confirmLoading = false
          })
    }

  }
}
</script>
